<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>method-override - 学习nodejs</title>
  

  <link rel="shortcut icon" href="../img/favicon.ico">

  
  <link href='https://fonts.googleapis.com/css?family=Lato:400,700|Roboto+Slab:400,700|Inconsolata:400,700' rel='stylesheet' type='text/css'>

  <link rel="stylesheet" href="../css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../css/theme_extra.css" type="text/css" />
  <link rel="stylesheet" href="../css/highlight.css">

  <script src="../js/jquery-2.1.1.min.js"></script>
  <script src="../js/modernizr-2.8.3.min.js"></script>
  <script type="text/javascript" src="../js/highlight.pack.js"></script>
  <script src="../js/theme.js"></script> 

  
</head>

<body class="wy-body-for-nav" role="document">

  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
      <div class="wy-side-nav-search">
        <a href=".." class="icon icon-home"> 学习nodejs</a>
        
      </div>

      <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
      <ul class="current">
    
        
            <li class="toctree-l1 ">
                <a class="" href="..">Home</a>
                
            </li>
        

    
        
            <li class="toctree-l1 ">
                <a class="" href="../morgan/">Morgan</a>
                
            </li>
        

    
        
            <li class="toctree-l1 ">
                <a class="" href="../connect-flash/">connect-flash</a>
                
            </li>
        

    
        
            <li class="toctree-l1 current">
                <a class="current" href="./">method-override</a>
                
                    <ul>
                    
                        <li class="toctree-l3"><a href="#method-override">method-override</a></li>
                        
                    
                        <li class="toctree-l3"><a href="#_1">示例</a></li>
                        
                    
                        <li class="toctree-l3"><a href="#one-more-thing">One more thing</a></li>
                        
                    
                    </ul>
                
            </li>
        

    
        
            <li class="toctree-l1 ">
                <a class="" href="../serve-favicon/">serve-favicon</a>
                
            </li>
        

    
</ul>

      </div>
      &nbsp;
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
        <a href="..">学习nodejs</a>
      </nav>

      
      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="breadcrumbs navigation">
  <ul class="wy-breadcrumbs">
    <li><a href="..">Docs</a> &raquo;</li>
    
    <li>method-override</li>
      <li class="wy-breadcrumbs-aside">
        
      </li>
  </ul>
  <hr/>
</div>
          <div role="main">
            <div class="section">
              <h2 id="method-override"><a href="https://www.npmjs.com/package/method-override">method-override</a></h2>
<p>通过HTTP请求头或参数来指定method.</p>
<p>原因是一些浏览器并不支持诸如PUT, DELETE这样的method，而我们在写路由的
时候为了语义化，通常会有PUT, DELETE这样的写法，故在路由真正开始处理
请求前，对method进行一次override.</p>
<h2 id="_1">示例</h2>
<pre><code>// npm i express method-override --save

var express = require('express')
var methodoverride = require('method-override')

var app = express();
app.use(methodoverride('_method'));
app.delete('/', function(req, res) {
  res.send(req.method);
});

app.listen(3001);
</code></pre>

<p>如下图所示，当请求时带上<code>_method=DELETE</code>时，即使我用的是POST方法，
也会被处理成DELETE.</p>
<p><img alt="ddd" src="../img/method-override.png" /></p>
<h2 id="one-more-thing">One more thing</h2>
<p>有人会担忧，那如果一个GET请求加上了_method参数，是不是就可以变成
POST/PUT/DELETE了啊？理论上是可以做到的。</p>
<p><code>method-override</code>有两个参数。</p>
<p><code>method-override(getter, options)</code>。</p>
<ul>
<li><code>getter</code>表示用来重写method的属性。如果是字符串，则这个属性就从querystring
  中找。如果是以<code>X-</code>开头的字符串，则往http header中找。如果是函数，则签名
  应当如：function (req, res) { return String}.</li>
<li><code>options.methods</code>表示可以被override的method.默认是['post']。</li>
</ul>
<p>由此可知，对于普通GET请求，默认是不会被override的。而POST请求可以被 
重写为任何method.</p>
            </div>
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="../serve-favicon/" class="btn btn-neutral float-right" title="serve-favicon"/>Next <span class="icon icon-circle-arrow-right"></span></a>
      
      
        <a href="../connect-flash/" class="btn btn-neutral" title="connect-flash"><span class="icon icon-circle-arrow-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <p>
    <!-- Copyright etc -->
    </p>
  </div>

  Built with <a href="http://www.mkdocs.org">MkDocs</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
	  
        </div>
      </div>

    </section>

  </div>

<div class="rst-versions" role="note" style="cursor: pointer">
    <span class="rst-current-version" data-toggle="rst-current-version">
        
      <span><a href="../connect-flash/" style="color: #fcfcfc;">&laquo; Previous</a></span>
      <span style="margin-left: 15px"><a href="../serve-favicon/" style="color: #fcfcfc">Next &raquo;</a></span>
    </span>
</div>

<!--
MkDocs version  : 0.12.2
Docs Build Date : 2015-04-27 16:40:46.080585
-->
</body>
</html>